<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/3
 * Time: 9:49
 */

namespace app\admin\controller;


use app\api\controller\Jpushmember;
use app\common\controller\Adminbase;
use think\Db;

class LoveList extends Adminbase
{

    /**
     * 爱心苑列表表
     * */
    public function index()
    {

        if($this->request->isAjax()){

            $where = [];
            //$where[] = ['love_list.status','>=','0'];

            $mobile = input('mobile');
            if($mobile){
                $where[] = ['a.mobile|a.username','like','%'.$mobile.'%'];
            }
            $admin = input('admin');
            if($admin){
                $where[] = ['b.mobile|b.username','like','%'.$admin.'%'];
            }
            $status = input('status');
            if($status){
                $where[] = ['love_list.status','=',$status];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }

            $order = "id desc";
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $list = Db::view('love_list','*')
                ->view('love_type','type_name','love_list.need_type = love_type.id','left')
                ->view('member a','mobile as member','a.id = love_list.user_id','left')
                ->view('member b','mobile as helper','b.id = love_list.help_id','left')
                ->view('admin','username','admin.userid = love_list.exame_user','left')
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('exame_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('sex', function ($value, $data) {
                    if($value == 1){
                        return "男";
                    }else{
                        return "女";
                    }
                })
                ->withAttr('type', function ($value, $data) {
                    if($value == 1){
                        return "求帮帮";
                    }else{
                        return "我帮帮";
                    }
                })
                ->withAttr('take_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->select();

            foreach ($list as $k => $v){
                if($v['status'] == 0){
                    $list[$k]['status_name'] = "未审核";
                }else if($v['status'] == 1){
                    $list[$k]['status_name'] = "已审核";
                }else if($v['status'] == 2){
                    $list[$k]['status_name'] = "接受帮帮";
                }else if($v['status'] == 3){
                    $list[$k]['status_name'] = "申请完成帮帮";
                }else if($v['status'] == 4){
                    $list[$k]['status_name'] = "完成帮帮";
                }else if($v['status'] == -1){
                    $list[$k]['status_name'] = "已拒绝";
                }
            }

            $total = Db::view('love_list','*')
                ->view('love_type','type_name','love_list.need_type = love_type.id','left')
                ->view('member a','mobile','a.id = love_list.user_id','left')
                ->view('member b','mobile as helper','b.id = love_list.help_id','left')
                ->view('admin','username','admin.userid = love_list.exame_user','left')
                ->where($where)->count();

            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);

        }else{
            return $this->fetch();
        }

    }


    /**
     * 帮帮通过
     * */
    public function pass()
    {

        $id = input('id');
        if(!$id){
            $this->error('参数获取有误');
        }
        $data['status'] = 1;
        $res = Db::name('love_list')->where(['id' => $id])->update($data);

        if($res){

            $jPush = new Jpushmember();

            $user = [];

            $info = Db::name('love_list')->where(['id' => $id])->field('user_id,help_id,type')->find();
            if($info['type'] == 1){
                array_push($user,$info['user_id']);
            }else{
                array_push($user,$info['help_id']);
            }
            $content = "您的帮助申请已通过";
            $result = $jPush->pushAlias($user,$content);

            if($result['http_code'] == 200) {
                $maps[] = ['apply_id', '=', $id];
                $maps[] = ['type', '=', 2];
                $helpCount = Db::name('send_msg')->where($maps)->find();
                if (count($helpCount)) {
                    $upData['is_check'] = 0;
                    $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                } else {

                    $inData = [
                        'user_id' => $info['user_id'],
                        'apply_id' => $id,
                        'title' => '您的帮助有了新进度',
                        'content' => "帮助申请已通过",
                        'create_time' => time(),
                        'is_check' => 0,
                        'type' => 2
                    ];
                    $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                }
            }
            $this->success('审核成功');
        }else{
            $this->error('审核失败');
        }

    }



    /**
     * 拒绝帮帮
     * */
    public function fail()
    {

        $id = input('id','');
        if($this->request->isPost()) {

            $id = input('id');
            $msg = input('beizhu','');
            if(!$id){
                $this->error('参数获取有误');
            }
            if(!$msg){
                $this->error('请填写拒绝帮助');
            }
            $data['status'] = -1;
            $data['exame_msg'] = $msg;
            $res = Db::name('love_list')->where(['id' => $id])->update($data);

            if ($res) {

                $jPush = new Jpushmember();

                $user = [];

                $info = Db::name('love_list')->where(['id' => $id])->field('user_id,help_id,type')->find();
                if ($info['type'] == 1) {
                    array_push($user, $info['user_id']);
                } else {
                    array_push($user, $info['help_id']);
                }
                $content = "您的帮助申请已拒绝";
                $result = $jPush->pushAlias($user, $content);

                if ($result['http_code'] == 200) {
                    $maps[] = ['apply_id', '=', $id];
                    $maps[] = ['type', '=', 2];
                    $helpCount = Db::name('send_msg')->where($maps)->find();
                    if (count($helpCount)) {
                        $upData['is_check'] = 0;
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->update($upData);
                    } else {

                        $inData = [
                            'user_id' => $info['user_id'],
                            'apply_id' => $id,
                            'title' => '您的帮助有了新进度',
                            'content' => "帮助申请已拒绝:" . $msg,
                            'create_time' => time(),
                            'is_check' => 0,
                            'type' => 2
                        ];
                        $reson = Db::name('send_msg')->where(['id' => $helpCount['id']])->insert($inData);
                    }
                }

//                $this->success('拒绝成功');
                $result = array("code" => 1, "msg" => "操作成功", "data" => [],"url" => "/admin/love_list/index");
            } else {
//                $this->error('拒绝失败');
                $result = array("code" => -1, "msg" => "操作成功", "data" => [],"url" => "/admin/love_list/index");
            }
            return json($result);
        }else{

            $this->assign('id',$id);
            return $this->fetch();

        }

    }

    /**
     * 完成帮帮
     * */
    public function finish()
    {

        $id = input('id');
        if(!$id){
            $this->error('参数获取有误');
        }
        $data['status'] = 3;
        $res = Db::name('love_list')->where(['id' => $id])->update($data);

        if($res){
            $this->success('完成成功');
        }else{
            $this->error('完成失败');
        }

    }


    /**
     * 详细信息
     * */
    public function info()
    {

        $id = input('id');
        $where[] = ['id','=',$id];

        $data = Db::name('love_list')->where($where)
            ->withAttr('type', function ($value, $data) {
                if($value == 1){
                    return "求帮帮";
                }else{
                    return "我帮帮";
                }
            })
            ->withAttr('status', function ($value, $data) {
                if($value == 1){
                    return "审核通过";
                }else if($value == 2){
                    return "接受帮帮";
                }else if($value == 3){
                    return "申请完成帮帮";
                }else if($value == 4){
                    return "完成帮帮";
                }else if($value == 0){
                    return "未审核";
                }else{
                    return "审核未通过";
                }
            })
            ->withAttr('need_type', function ($value, $data) {
                if($value == 1){
                    return "物品";
                }else{
                    return "问答";
                }
            })
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('exame_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->withAttr('take_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->find();
        $this->assign('data',$data);
        return $this->fetch();

    }

    /**
     * 解绑操作
     * */
    public function unlock()
    {

        $id = input('id');

        if(!$id){
            $this->error('数据获取有误');
        }

        $type = Db::name('love_list')->where(['id' => $id])->value('type');

        if($type == 1){
            $data = [
                'help_id' => '',
                'help_name' => '',
                'help_danwei' => '',
                'help_sex' => '',
                'help_bron' => '',
                'help_mobile' => '',
                'help_political' => '',
                'help_political_info' => '',
                'help_info' => '',
                'status' => 1
            ];
        }else{
            $data = [
                'user_id' => '',
                'realname' => '',
                'danwei' => '',
                'rudang_date' => '',
                'sex' => '',
                'mobile' => '',
                'channel' => '',
                'need_type' => '',
                'status' => 1,
            ];
        }

        $res = Db::name('love_list')->where(['id' => $id])->update($data);
        if($res){
            $this->success('解绑成功');
        }else{
            $this->error('解绑失败');
        }

    }


}